#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'baixue'

from ctypes import *


adk_lib = windll.LoadLibrary('8144.dll')


def initial(ManualId=0):
    CardId = c_int()
    ManualId = c_short(ManualId)
    err = adk_lib._8144_initial(byref(CardId), ManualId)
    check_error(err)
    return CardId.value




#------------------Error Code Def-------------------#
ErrorMsg = ("ERR_OSVersion",
            "ERR_OpenDriverFailed",
            "ERR_InsufficientMemory",
            "ERR_DeviceNotInitial",
            "ERR_NoDeviceFound",
            "ERR_CardIdDuplicate",
            "ERR_DeviceAlreadyInitialed",
            "ERR_InterruptNotEnable",
            "ERR_TimeOut",
            "ERR_ParametersInvalid",
            "ERR_SetEEPROM",
            "ERR_GetEEPROM",
            "ERR_MotionBusy"
            )

WarningMsg = ("WAR_AccDecTimeTooLarge",
              "WAR_AccDecTimeTooSmall",
              "WAR_RampDownPointExceed",
              "WAR_DistanceTooShort"
              )

ERR_Win32Error = -1000

class Adk_Error(Exception):
    def __init__(self, errcode):
        self.errcode = errcode

    def __str__(self):
        if self.errcode in (1,2,3,4):
            return WarningMsg[self.errcode-1]
        elif self.errcode >= -13 and self.errcode <= -1:
            return ErrorMsg[-self.errcode-1]
        else:
            return format("Win32Error:{}" , str(-(self.errcode + ERR_Win32Error)))


def check_error(errcode):
    print errcode
    if errcode != 0:
        raise Adk_Error(errcode)
        
initial()
